﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.ComponentModel;

namespace JYM.Common
{
    /// <summary>
    /// 枚举工具类
    /// </summary>
    public class EnumHelper
    {


        /// <summary>
        /// 检索枚举描述, 示例：
        /// using System.ComponentModel;
        /// enum UserColours{
        /// [Description("Bright Pink")]
        /// BrightPink = 2
        /// }
        /// 然后当你在枚举通过，它将检索说明，
        /// 调用：EnumHelper.GetDescription(UserColours.BrightPink);
        /// </summary>
        /// <param name="en">枚举项</param>
        /// <returns>枚举类型项的Description描述说明，没有侧返回当前项的名称</returns>
        public static string GetDescription(Enum en)
        {
            Type type = en.GetType();

            MemberInfo[] memInfo = type.GetMember(en.ToString());

            if (memInfo != null && memInfo.Length > 0)
            {
                object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

                if (attrs != null && attrs.Length > 0)
                {
                    return ((DescriptionAttribute)attrs[0]).Description;
                }
            }

            return en.ToString();
        }

        /// <summary>
        /// 把文本值转换指定类型的枚举项
        /// </summary>
        /// <typeparam name="TEnum">枚举对像</typeparam>
        /// <param name="text">文本值</param>
        /// <returns>枚举对像</returns>
        public static TEnum Parse<TEnum>(string text) where TEnum : struct
        {
            return Parse<TEnum>(text, true, default(TEnum));
        }

        /// <summary>
        /// 把文本值转换指定类型的枚举项
        /// </summary>
        /// <typeparam name="TEnum">枚举对像</typeparam>
        /// <param name="text">文本值</param>
        /// <param name="throwException">无法转换是是否抛出异常</param>
        /// <param name="defaultValue">当无法转换是的默认值</param>
        /// <returns>枚举对像</returns>
        public static TEnum Parse<TEnum>(string text, bool throwException, TEnum defaultValue) where TEnum : struct
        {
            string sEnum = text;
            if (!Enum.IsDefined(typeof(TEnum), text))
            {
                sEnum = text.ToLower();
                if (!Enum.IsDefined(typeof(TEnum), sEnum))
                {
                    sEnum = text.ToUpper();
                    if (!Enum.IsDefined(typeof(TEnum), sEnum))
                    {
                        if (throwException)
                            throw new ArgumentException(string.Format("{0}中不包含名为\"{1}\"的项，请注意项名称的大小写。", typeof(TEnum).Name, text));
                        else
                            return defaultValue;
                    }
                }
            }
            TEnum t = (TEnum)Enum.Parse(typeof(TEnum), text, true);
            return t;
        }

        /// <summary>
        /// 换转成枚举对像包含的项的列表
        /// </summary>
        /// <typeparam name="T">枚举对像</typeparam>
        /// <returns>枚举对像包含的项的列表</returns>
        public static List<T> ConvertToList<T>()
        {
            List<T> values = new List<T>();
            Array array = Enum.GetValues(typeof(T));
            foreach (T item in array)
                values.Add(item);

            return values;
        }

        /// <summary>
        /// 获取枚举对像包含名称列表
        /// </summary>
        /// <typeparam name="T">枚举对像</typeparam>
        /// <returns>枚举对像包含名称列表</returns>
        public static string[] GetNames<T>()
        {
            return Enum.GetNames(typeof(T));
        }

        /// <summary>
        /// 获取枚举对像的值
        /// </summary>
        /// <typeparam name="T">枚举对像</typeparam>
        /// <typeparam name="U">一般为int</typeparam>
        /// <returns></returns>
        public static List<U> GetValues<T, U>()
        {
            List<U> values = new List<U>();
            Array array = Enum.GetValues(typeof(T));
            foreach (U item in array)
                values.Add(item);
            return values;
        }



    }
}
